package cn.iocoder.yudao.module.polling.service.pollingplan;

import cn.iocoder.yudao.module.polling.controller.admin.pollingcategories.vo.EquVo;
import cn.iocoder.yudao.module.polling.controller.admin.pollingplan.vo.*;
import cn.iocoder.yudao.module.polling.convert.pollingplan.PollingPlanConvert;
import cn.iocoder.yudao.module.polling.dal.dataobject.pollingplan.PollingPlanDO;
import cn.iocoder.yudao.module.polling.dal.mysql.pollingplan.PollingPlanMapper;
import cn.iocoder.yudao.module.polling.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;


import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 巡检计划 Service 实现类
 *
 * @author 左达成
 */
@Service
@Validated
public class PollingPlanServiceImpl implements PollingPlanService {

    @Resource
    private PollingPlanMapper pollingPlanMapper;

    @Override
    public Integer createPollingPlan(PollingPlanCreateReqVO createReqVO) {
        // 插入
        PollingPlanDO pollingPlan = PollingPlanConvert.INSTANCE.convert(createReqVO);
        //

        pollingPlanMapper.insert(pollingPlan);
        // 返回
        return pollingPlan.getId();
    }

    @Override
    public void updatePollingPlan(PollingPlanUpdateReqVO updateReqVO) {
        // 校验存在
        this.validatePollingPlanExists(updateReqVO.getId());
        // 更新
        PollingPlanDO updateObj = PollingPlanConvert.INSTANCE.convert(updateReqVO);
        pollingPlanMapper.updateById(updateObj);
    }

    @Override
    public void deletePollingPlan(Integer id) {
        // 校验存在
        this.validatePollingPlanExists(id);
        // 删除
        pollingPlanMapper.deleteById(id);
    }

    private void validatePollingPlanExists(Integer id) {
        if (pollingPlanMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.STANDARD_NOT_EXISTS);
        }
    }

    @Override
    public PollingPlanDO getPollingPlan(Integer id) {
        return pollingPlanMapper.selectById(id);
    }

    @Override
    public List<PollingPlanDO> getPollingPlanList(Collection<Integer> ids) {
        return pollingPlanMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PollingPlanDO> getPollingPlanPage(PollingPlanPageReqVO pageReqVO) {
        return pollingPlanMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PollingPlanDO> getPollingPlanList(PollingPlanExportReqVO exportReqVO) {
        return pollingPlanMapper.selectList(exportReqVO);
    }

    @Override
    public List<PollingPlanInVo> selectPlanList(PollingPlanInVo pollingPlanInVo) {
        return pollingPlanMapper.selectPlanList(pollingPlanInVo);
    }

    @Override
    public List<PollingPlanInDao> selectDetailsList(PollingPlanInDao pollingPlanInDao) {
        return pollingPlanMapper.selectDetailsList(pollingPlanInDao);
    }

    @Override
    public List<EquVo> selectUserByPlanId(int id) {
        return pollingPlanMapper.selectUserByPlanId(id);
    }

}
